package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.result.PageResult;
import com.itheima.common.result.Result;
import com.itheima.dto.YddAdvimgDTO;
import com.itheima.dto.YddAdvposDTO;
import com.itheima.mapper.YddAdvimgMapper;
import com.itheima.mapper.YddAdvposMapper;
import com.itheima.pojo.YddAdvimg;
import com.itheima.pojo.YddAdvpos;
import com.itheima.service.IYddAdvposService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IYddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos> implements IYddAdvposService {
    @Autowired
    private YddAdvposMapper yddAdvposMapper;
    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;
    /**
     * 广告位列表分页
     * @param dto
     * @return
     */
    @Override
    public PageResult listAdvpos(YddAdvposDTO dto) {
        //非空判断
        if(dto.getLimit()==null){
            dto.setLimit("5");
        }
        if(dto.getPage()==null){
            dto.setPage("1");
        }
        //开启分页
        PageHelper.startPage(Integer.parseInt(dto.getPage()),Integer.parseInt(dto.getLimit()));
        //获取广告位描述、广告位名称
        String advposdesc = dto.getAdvposdesc();
        String advposname = dto.getAdvposname();
        //根据广告位描述、广告位名称获取符合条件的数据
        Page<YddAdvpos> yddPage = yddAdvposMapper.listAdvpos(advposdesc,advposname);
        List<YddAdvpos> yddList = yddPage.getResult();
        //封装返回结果
        PageResult pageResult = new PageResult();
        //获取总条数
        pageResult.setCount(yddPage.size());
        //获取当前页数
        pageResult.setCurrenPage(Integer.parseInt(dto.getPage()));
        //获取数据.
        pageResult.setData(yddList);
        //获取当前页面显示数量
        pageResult.setPageSize(Integer.parseInt(dto.getLimit()));
        //获取总页数
        pageResult.setTotalPages(yddPage.getTotal());
        return pageResult;
    }
    /**
     * 广告位修改
     * @param yddAdvpos
     * @return
     */
    @Override
    public void updateAdvpos(YddAdvpos yddAdvpos) {
        Integer id = yddAdvposMapper.updateAdvpos(yddAdvpos);
    }
    /**
     * 广告位详情
     * @param id
     * @return
     */
    @Override
    public YddAdvpos rowInfo(Integer id) {
        YddAdvpos yddAdvpos = yddAdvposMapper.selectById(id);
        return yddAdvpos;
    }
    /**
     * 删除广告位
     * @param id
     * @return
     */
    @Override
    public void deleteAdvpos(Integer id) {
        //删除后获取广告位id
        int adId = yddAdvposMapper.deleteById(id);
        //删除广告位对应广告图的数据
        QueryWrapper lqw = new QueryWrapper();
        lqw.eq("advimgpos",adId);
        yddAdvimgMapper.delete(lqw);
    }


}
